home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1233 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.8 KB

  1. Path: news.tuwien.ac.at!news
  2. From: Joachim Fabini <jo@vmars.tuwien.ac.at>
  3. Newsgroups: comp.lang.c,comp.windows.x.motif
  4. Subject: [Q:] flex input from string?
  5. Date: Fri, 12 Jan 1996 14:22:44 +0100
  6. Organization: RTSystems
  7. Distribution: inet
  8. Message-ID: <30F660A4.41C6@vmars.tuwien.ac.at>
  9. NNTP-Posting-Host: tunix.vmars.tuwien.ac.at
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b3 (X11; I; OSF1 V2.1 alpha)
  14.  
  15. Missing a dedicated (f)lex newsgroup, I'm posting this 
  16. question to this newsgroup(s), hoping that someone can help me.
  17.  
  18.     I wrote a X-Motif-App with some dialogs and input forms.
  19. Flex-bison is used to read input from a file and fill in the different
  20. input form's fields. 
  21.  
  22.     My question: (f)lex reads by default from the FILE* yyin,
  23. defaulting to stdin. How can I read input from a _STRING_ ? Say, I get 
  24. the text field's value and pass it as input to the scanner, that 
  25. returns some token and I know exactly if the input was correct or not.
  26.     It's nonsense to do the whole checking in C, that only 
  27. introduces unnecessary redundancy (if I change the scanner/parser,
  28. I have to change also the checking routine).
  29.  
  30.    At the moment I have three solutions, but none of them seems
  31.  to me to be the ultimate one:
  32.     1. Write string to temporary file and open file as input stream 
  33.          (Worst approach ever seen)
  34.     2. fork(), open two pipes, and the child process simply writes
  35.        the values it gets from the first pipe to the second.
  36.        I.e a fputs/fgets pair in the application. Solution involves
  37.        overhead... and works _only_ on Unix-Systems.
  38.     3. I doubt if this really works (platform independent):
  39.        Defining YY_INPUT (as described in the flex man page) to:
  40.  
  41.         #define YY_INPUT(buf, result, max_size)\
  42.          if((!global_ptr) || (!global_ptr[0])) { \
  43.                result = YY_NULL; \
  44.              }\
  45.              else { \
  46.                strncpy(buf, global_ptr, max_size); \
  47.                result = strlen(buf); \
  48.                global_ptr += result; \
  49.              }
  50.     
  51.       This is the solution I'm using for the moment, (reading from
  52.       a globally available pointer) and this solution _does_ work.
  53.  
  54.       The reason why I'm in doubt: I don't know how a unput(c) 
  55.       or some other function should work with this approach (as
  56.       there is no stream to put the character back to)... Maybe 
  57.       there are also some other problems I just ignore?
  58.  
  59.   If someone experienced the same problem, has done the job in a
  60.   different manner or just wants to give his oppinion (solution #3
  61.   should work/ doesn't work/ is junk) please re-post and/or send  
  62.   mail to jo@vmars.tuwien.ac.at
  63.  
  64.     Thanks in advance 
  65.  
  66. --Jo  
  67. ******************************************************************
  68. Joachim Fabini
  69. jo@vmars.tuwien.ac.at
  70. e9027595@stud1.tuwien.ac.at
  71. ******************************************************************
  72.